package com.xr.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xr.mapper.MemberMapper;
import com.xr.pojo.Member;
import com.xr.pojo.MemberExample;
import com.xr.query.QueryPageBean;
import com.xr.result.PageResult;
import com.xr.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service(interfaceClass = MemberService.class)
public class MemberServiceImpl implements MemberService {
    @Autowired
    private MemberMapper memberMapper;

    @Transactional
    @Override
    public Member login(String telephone) {
        MemberExample me = new MemberExample();
        MemberExample.Criteria mec = me.createCriteria();
        mec.andPhonenumberEqualTo(telephone);
        List<Member> list = memberMapper.selectByExample(me);
        if (list!=null && list.size()>0){
            //是，直接返回会员信息
            return list.get(0);
        }
        //不是，需要完成注册，并且获取会员id，将会员信息返回
        //新增添加会员的业务方法，直接调用，这里暂时还没写
        Member member=new Member();
        member.setPhonenumber(telephone);
        member.setRegtime(new Date());
        member.setRemark("手机快速注册"); //标记
        memberMapper.insertSelective(member);//完成会员注册
        return member;
    }

    @Override
    public void add(Member member) {
        memberMapper.insertSelective(member);
    }

    @Override
    public List<Integer> getMemberReport(List<String> months) {
        List<Integer> regTime = new ArrayList<>();
        months.forEach(s -> {
            Integer c = memberMapper.findMemberCountBeforeDate(s+".1");
            regTime.add(c);
        });
        return regTime;
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<Member> page = memberMapper.selectByFindString(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void addMember(Member member) {
        member.setRegtime(new Date());
        memberMapper.insertSelectiveS(member);
    }

    @Override
    public void updMember(Member member) {
        memberMapper.updateByPrimaryKeySelective(member);
    }

    @Override
    public void delMember(Integer id) {
        memberMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void queryById(Integer id) {
        memberMapper.selectByPrimaryKey(id);
    }
}
